package com.fourcitizens.playas

import com.googlecode.concurrentlinkedhashmap.ConcurrentLinkedHashMap.Values;
import com.sun.jdi.connect.AttachingConnector;

class BeachException {
	String message
	Beach beach
}

class JSONBeach {
	
	String country
	String province
	String town
	
	// TODO: delete
	double latitude
	double longitude
	
	// Basic info
	String name
	String description
	int length
	int mediumWidth
	boolean promenade = false
	boolean anchorageArea = false
	boolean nudist = false
	
	// Beach features
	String occupation
	String urbanization
	String composition
	String sandType
	String bathingConditions
	
	//Environmental info
	boolean vegetation = false
	boolean blueFlag = false
	boolean protectedArea = false
	
	//Accesibility info
	boolean accessSignaling = false
	boolean disabledAccess = false
	
	// Services info
	boolean toilets = false
	boolean showers = false
	boolean phone = false
	boolean paperBin = false
	boolean cleaningService = false
	boolean gateways = false
	boolean umbrellasRental = false
	boolean sunbedRental = false
	boolean yatchRental = false
	boolean spaKiosks = false
	boolean yatchClub = false
	boolean divingArea = false
}

class BeachService {

    static transactional = true
	def countryService
	def provinceService
	def townService

    Beach createBeach(long townId, String beachName, Map beachAttributes) {
	 	def beach = new Beach(name : beachName)
		fillBeachAtributes(beach,beachAttributes)

		def town = Town.findById(townId)
		
		if (town) {
			town.addToBeaches(beach)
			
			if (!town.save(failOnError: true))
				throw new BeachException(message : "Could not create the beach", beach)
		}
		return beach
    }
	
 	Beach createBeach(Town town, String beachName, Map beachAttributes) {
		 return createBeach(town.id, beachName, beachAttributes)
	}
	 
	Beach createBeach(String countryName, String provinceName, String townName, String beachName, Map beachAttributes) {

		def country = countryService.getCountry(countryName)
		
		if (!country){
			country = countryService.createCountry(countryName)
		}
		
		def province = provinceService.getProvinceInCountry(country,provinceName) 
		
		if (!province) {
			province = provinceService.createProvince(country,provinceName)
			country.addToProvinces(province)
		}
		
		def town = townService.getTownInProvince(province, townName)
		
		if (!town) {
			town = townService.createTown(province,townName)
			province.addToTowns(town)
		}
		
		
		def beach = getBeachInTown(town,beachName)
		
		if (!beach) {
			beach = new Beach(name : beachName)
			fillBeachAtributes(beach,beachAttributes)
			town.addToBeaches(beach)
		}
		
		country.save()
		
		return beach
	}
	
	Beach[] getBeaches() {
		return Beach.list()
	}
	
	Beach getBeach(long beachId) {
		return Beach.findById(beachId)
	}

	Beach getBeachInTown(Town town, String beachName) {

		def beach = Beach.createCriteria().list {
			and {
				eq('town',town)
				eq('name',beachName)
			}
		}

		return beach[0]
	}
	 
	private fillBeachAtributes(Beach beach, Map beachAttributes) {
		
		for (attribute in beachAttributes) {
			
			def key = attribute.key
			def value = attribute.value
			
			if(key.equals('length') ||  key.equals('mediumWidth'))
			{
				value = value as Integer
			}
			
		
			if(key.equals('latitude') || key.equals('longitude'))
			{
				
				if (value) {
					try {
						value = value as Double
					}
					catch (Exception e) {
						println value
						value = ""
						//e.printStackTrace()
						//println beach.toString()
					}
					
				}
				
				
			}
			
			beach.putAt(key, value)
			
		}
	}
	
	JSONBeach convertToJSONBeach(Object beach) {
		
		def jsonBeach = new JSONBeach()

		jsonBeach.properties.each {k,v ->
		  if(k!="class" && k!="metaClass" && k!="province" && k!="town" && k!="country") jsonBeach."${k}" = beach."${k}"
		}
		
		jsonBeach.country = beach.town.province.country.name
		jsonBeach.province = beach.town.province.name
		jsonBeach.town = beach.town.name
		
		return jsonBeach
		
	}
}
